package com.zzxx.photograph.dao.impl;

import com.zzxx.photograph.dao.CategoryDao;
import com.zzxx.photograph.domain.Category;
import com.zzxx.photograph.domain.Img;
import com.zzxx.photograph.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class CategoryDaoImpl implements CategoryDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public List<Category> findAll() {
        String sql = "select * from img_category";
        return template.query(sql, new BeanPropertyRowMapper<>(Category.class));
    }

    @Override
    public List<Img> queryImgByCid(int cid, int start, int pageSize) {
        String sql = "select * from img where cid=? limit ?,?";
        return template.query(sql, new BeanPropertyRowMapper<>(Img.class), cid, start, pageSize);
    }

    @Override
    public int queryCountByCid(int cid) {
        String sql = "select count(*) from img where cid=?";
        return template.queryForObject(sql, Integer.class, cid);
    }

    @Override
    public int findCidImgByIid(int iid) {
        String sql = "select cid from img where iid=?";
        return template.queryForObject(sql, Integer.class, iid);

    }

    @Override
    public List<Img> findImgsByCid(int cid, int iid) {
        String sql = "select * from img where cid=? and iid!=? ";
        return template.query(sql, new BeanPropertyRowMapper<>(Img.class), cid,iid);
    }

    @Override
    public Integer findCidByCname(String cname) {
        String sql="select cid from img_category where cname=?";
        return template.queryForObject(sql,Integer.class,cname);
    }
}
